﻿var Dimok;
if( !Dimok )Dimok = typeof(Type) != 'undefined' ? Type.registerNamespace("Dimok") : {};
if( !Dimok.__namespace )Dimok.__namespace = true;
Dimok.Events = new Dimok$Events();
function Dimok$Events(){
	this.attachEvent = function attachEvent(eventName,eventHandler,ID){
		var oEvents = _eventsObject();
		if( !oEvents )try{oEvents = _eventsObject(true);}catch(e){}
		if( !oEvents )return;
		if( typeof(ID) != 'object' )ID = {ID:ID};
		ID.self = self;
		var eo = {name:eventName,callBack:eventHandler,eventObject:ID,self:self}
		_pushEvent(oEvents,eo);
		///Loacls
		function _pushEvent(oe,eo){
			var name,n;
			for( name in oe )
				n = parseInt(name.substr(1));
			try{oe["_"+((n||0)+1)] = eo;
			}catch(e){status="_pushEvent:"+e.description}
		}
	}
	this.fireEvent = function fireEvent(eventName,ID){
		fireEvent.eventID = (fireEvent.eventID||0)+1;
		var oEvents = _eventsObject();
		if( !oEvents )return;
		if( typeof(ID) != 'object' )ID = {ID:ID};
		var name;
		try{
			for( name in oEvents )
				try{
					if( oEvents[name].name == eventName && oEvents[name].self != self ){
						if( oEvents[name].eventObject.ID === undefined || ID.ID == oEvents[name].eventObject.ID ){
							var foo = oEvents[name].callBack;
//							if( foo.eventID == fireEvent.eventID )continue;
//							foo.eventID = fireEvent.eventID;
							foo(mergeObjects(ID,oEvents[name].eventObject));
						}
					}
				}catch(e){///alert("Event:"+eventName+"\n\n"+(e.description||"Unknown Error."))
					delete oEvents[name];
				}
		}catch(e){///alert("Event Manager:\n\n"+e.description)
			oEvents = {};
		}
		function mergeObjects(objTo,objFrom){
			for( var n in objFrom )
				if( objTo[n] === undefined )
					objTo[n] = objFrom[n];
			return objTo;
		}
	}
	function _eventsObject(create){
		if( create && !_eventsObject() )
			_top().oEvents = {};
		try{return _top().oEvents;
		}catch(e){return top.oEvents;}
		//locals
		function _top(){
			try{
				var w = top;
				while( w.location.href != w.top.location.href )w = w.top;
				if( w.opener )w = w.opener;
				if( w.dialogArguments && w.dialogArguments.opener )w = w.dialogArguments.opener;
				while( w.location.href != w.top.location.href )w = w.top;
				return w;	
			}catch(e){
				status = "Events:"+e.description;
				return top;
			}
		}
	}
}

var TEST_EVENT = "TEST_EVENT";
function     fireTestEvent(applID){       Dimok.Events.fireEvent(TEST_EVENT,applID)};
function attachToTestEvent(func,applID){Dimok.Events.attachEvent(TEST_EVENT,func,applID);}

var APPL_INFO_CHANGED = "applInfoChanged";
function     fireApplInfoChanged(applID){       return Dimok.Events.fireEvent(APPL_INFO_CHANGED,applID)};
function attachToApplInfoChanged(func,applID){Dimok.Events.attachEvent(APPL_INFO_CHANGED,func,applID);}

var APPLICANT_SELECTED = "APPLICANT_SELECTED";
function     fireApplicantSelected(applID){     return Dimok.Events.fireEvent(APPLICANT_SELECTED,applID)};
function attachToApplicantSelected(func,applID){Dimok.Events.attachEvent(APPLICANT_SELECTED,func,applID);}

var LIST_LOAD_NEXT = "LIST_LOAD_NEXT";
function     fireListLoadNext(applID){       Dimok.Events.fireEvent(LIST_LOAD_NEXT,applID)};
function attachToListLoadNext(func,applID){Dimok.Events.attachEvent(LIST_LOAD_NEXT,func,applID);}

var FILTER_CHANGE = "FILTER_CHANGE";
function   fireFilterChange(applID){     Dimok.Events.fireEvent  (FILTER_CHANGE,applID)};
function attachToFilterChange(func,applID){Dimok.Events.attachEvent(FILTER_CHANGE,func,applID);}

var LIST_REFRESH = "LIST_REFRESH"
function     fireListRefresh(applID){       Dimok.Events.fireEvent(LIST_REFRESH,applID)};
function attachToListRefresh(func,applID){Dimok.Events.attachEvent(LIST_REFRESH,func,applID);}

var SEND_TO_MENU = "sendToMenu";
function     fireSendToMenu(applID){       Dimok.Events.fireEvent(SEND_TO_MENU,applID)};
function attachToSendToMenu(func,applID){Dimok.Events.attachEvent(SEND_TO_MENU,func,applID);}

var CREDIT_RE_PULLED = "CREDIT_RE_PULLED";
function     fireCreditRepulled(applID){       Dimok.Events.fireEvent(CREDIT_RE_PULLED,applID)};
function attachToCreditRepulled(func,applID){Dimok.Events.attachEvent(CREDIT_RE_PULLED,func,applID);}

var DECISION_INFO_CHANGED = "DECISION_INFO_CHANGED";
function     fireDecisionInfoChanged(applID){       Dimok.Events.fireEvent(DECISION_INFO_CHANGED,applID)};
function attachToDecisionInfoChanged(func,applID){Dimok.Events.attachEvent(DECISION_INFO_CHANGED,func,applID);}

var VER_TREE_CHANGED = "VER_TREE_CHANGED";
function     fireVerTreeChanged(applID){ Dimok.Events.fireEvent   (VER_TREE_CHANGED,applID)};
function attachToVerTreeChanged(func,applID){Dimok.Events.attachEvent(VER_TREE_CHANGED,func,applID);}

var RELOAD_VER_TREE = "ReloadVerTree";
function   fireOnReloadVerTree(applID){     Dimok.Events.fireEvent  (RELOAD_VER_TREE,applID)};
function attachToReloadVerTree(func,applID){Dimok.Events.attachEvent(RELOAD_VER_TREE,func,applID);}

